package Thread;
class Counter{  // 创建一个新的类
    public int count = 0;
    private Object locker = new Object();
    // 创建一个新的方法
    synchronized public void increase(){
        count++;
    }
    synchronized public void increase2(){
        count++;
    }
}
// 线程安全演示
public class Demo8 {

    public static void main(String[] args) {
        Counter counter = new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                counter.increase();
            } 
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                counter.increase2();
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(counter.count);

    }
}
